package com.eqdd.yiqidian.ui.discover.makerspace

import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.base.BaseActivity
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.http.JsonConverter
import com.eqdd.yiqidian.kotlin.deleteDialog
import com.eqdd.yiqidian.ui.companyspace.ProductInfoActivity
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.FlowableBody
import com.shizhefei.mvc.MVCCoolHelper
import io.reactivex.Flowable
import kotlinx.android.synthetic.main.activity_recyclerview_refresh.*
import org.jetbrains.anko.startActivity

/**
 * Created by jhj on 18-8-13.
 */
class CollectProductActivity : BaseActivity() {
    override fun initBinding() {
        setContentView(R.layout.activity_recyclerview_refresh)
        initTopTitleBar(View.VISIBLE, "收藏的产品")
    }

    override fun initData() {
        var pageNum = 0
        val list = arrayListOf<ProductBean>()
        val mvcHelper = MVCCoolHelper<List<ProductBean>>(coolRefreshView)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.addItemDecoration(DividerLineItemDecoration(this))
        val slimAdapterEx = SlimAdapterEx.create()
                .register<ProductBean>(R.layout.list_item_173, SlimInjector<ProductBean> { data, injector ->
                    injector?.image(R.id.imageView, data?.productImage)
                            ?.text(R.id.tv_content, data?.productName)
                            ?.text(R.id.tv_price, "￥" + data?.productPrice + "元")
                            ?.gone(R.id.tv_company)
                            ?.visible(R.id.recyclerView)
                })
                .attachTo(recyclerView)
                .updateData(list as List<ProductBean>?) as SlimAdapterEx<ProductBean>?

        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener { recyclerView, position, v ->
                    val data = slimAdapterEx?.getDataItem(position)
                    startActivity<ProductInfoActivity>(Config.ID to data?.productId)
                }
                .setOnItemLongClickListener { recyclerView, position, v ->
                    val data = slimAdapterEx?.getDataItem(position)
                    deleteDialog(HttpConfig.BASE_URL + HttpConfig.MAKER_COLLECT_CANCEL, "是否取消收藏“${data?.productName}”？",
                            "collectId" to data?.Id, "type" to 1, isFinish = false) {
                        mvcHelper.refresh()
                    }
                    true
                }

        val dataSource = ModelRx2DataSource<ProductBean>(ModelRx2DataSource.OnLoadSource<ProductBean> { page, register ->
            if (page == 1) {
                pageNum = 0
            }
            OkGo.post<HttpResult<List<ProductBean>>>(HttpConfig.BASE_URL + HttpConfig.MAKER_COLLECT_PRODUCT)
                    .params("userGuid", user.guid)
                    .params("page", pageNum)
                    .converter(object : JsonConverter<HttpResult<List<ProductBean>>>() {
                        override fun test() {
                            super.test()
                        }
                    })
                    .adapt(FlowableBody<HttpResult<List<ProductBean>>>())
                    .flatMap({ result ->
                        if (pageNum == 0) {
                            mvcHelper.isAutoLoadMore = true
                        }
                        pageNum = result.page
                        Flowable.just(if (result.items == null) arrayListOf<ProductBean>() else result.items)
                    })
        })

        mvcHelper.setDataSource(dataSource)
        mvcHelper.adapter = slimAdapterEx
        mvcHelper.refresh()

    }

    override fun setView() {

    }


    class ProductBean {
        val Id: Int = 0
        val company: String = ""
        val comLogo: String = ""
        val companyId: Int = 0
        val productName: String = ""
        val productImage: String = ""
        val productPrice: Double = 0.0
        val productId: Int = 0
    }
}